package com.asiainfo.iov.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.asiainfo.iov.configs.ConstantsME;
import com.asiainfo.iov.configs.InterfaceConfig;
import com.asiainfo.iov.entity.MessageInfo;
import com.asiainfo.iov.networkModel.HttpUtil;
import com.framework.utils.PreferencesHelper;
import com.framework.utils.ScreenUtils;
import com.framework.utils.ToastUtil;
import com.framework2.customviews.TitleView;
import com.framework2.util.CustomLoadingDialogUtils;
import com.framework2.util.PicToastUtil;
import com.yaxin.yyt.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改昵称
 *
 * @author YobertJomi
 * className ModifyNickActivity
 * created at  2017/3/27  18:22
 */
public class ModifyNickActivity extends BaseActivity {
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.et_nick)
    EditText etNick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_nick);
        ButterKnife.bind(this);
        ScreenUtils.getInstance().setTranslucentStatus(this, true);
        ScreenUtils.getInstance().setStatusBarTintColor(this,
                getResources().getColor(R.color.white));
        initView();
    }

    //eventBus通知新消息
    @Subscribe(threadMode = ThreadMode.MAIN, tag = "newMessage")
    public void receivedNewMessage(MessageInfo info) {
        titleView.showRightMarkIcon(true);
    }

    private void initView() {
        titleView.setTitle("修改昵称");
        if (!TextUtils.isEmpty(getIntent().getStringExtra(ConstantsME.nick))) {
            etNick.setText(getIntent().getStringExtra(ConstantsME.nick));
            etNick.setSelection(getIntent().getStringExtra(ConstantsME.nick).length());
        }
    }

    /**
     * 请求更改昵称
     */
    private void requestModifyNick(@NonNull String nick) {
        HttpUtil.getInstance().requestModifyNick(InterfaceConfig.modifyNick, nick,
                new HttpUtil.OnRequestResult<String>() {
                    @Override
                    public void onSuccess(String... s) {
                        CustomLoadingDialogUtils.dismissProcessDialog();
                        PreferencesHelper.getInstance().putInfo(ConstantsME.nick, etNick.getText().toString());
                        ToastUtil.getInstance().showToast("修改昵称成功");
                        finishActivity();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        CustomLoadingDialogUtils.dismissProcessDialog();
                        ToastUtil.getInstance().showToast(TextUtils.isEmpty(msg) ? "修改昵称失败" : msg);
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != titleView)
            titleView.showRightMarkIcon(PreferencesHelper.getInstance().getBooleanData(ConstantsME.hasNewMessage));
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sure://save
                if (TextUtils.isEmpty(etNick.getText())) {
                    PicToastUtil.getInstance().showToast(this, "昵称不能为空");
                } else {
                    CustomLoadingDialogUtils.showProcessDialog(this, "修改昵称");
                    requestModifyNick(etNick.getText().toString());
                }
                break;
            default:
                break;
        }
    }
}
